#include "termlMsg_handle.h"
#include "msg_assem.h"

/***************************/
static  UINT8 *p_SetArgsHead = NULL;
static  UINT8 *p_SetArgsTail = NULL;
static  INT8 setArgsFinishFlag = FALSE;
//static  UINT8 *s_pRouteBufHead=NULL;
//static  UINT8 *s_pRouteBufTail=NULL;
//extern VEHICLE_ARGS_STR g_vehicleArgs;

//0x0001
/*INT32 term_common_ack(JT808_MSG_PKG *pMsg)
{
	UINT8 *p_body, result;
	UINT16 seqNum, rpyId;

	if(pMsg == NULL || pMsg->pbody == NULL)
		return -1;
	p_body = pMsg->pbody;

	seqNum = (UINT16)ntohs(*(UINT16 *)p_body);
	p_body += 2;
	rpyId = (UINT16)ntohs(*(UINT16 *)p_body);
	p_body += 2;
	result = *(UINT8 *)p_body;

	return result;	
}
*/
//0x0002

//0x0100
//TODO
INT32 term_register(UINT8 *pbMsg)
{
	UINT8 *p_body = NULL;
	INT32 ix;

	if(pbMsg == NULL)
		return -1;
	p_body = pbMsg;

	g_vehicleArgs.ProvinceId = (UINT16)ntohs(*(UINT16 *)p_body);
	p_body += 2;
	g_vehicleArgs.CityId = (UINT16)ntohs(*(UINT16 *)p_body);
	p_body += 2;
	memcpy(g_installArgs.mfrId, p_body, 5);
	p_body += 5;
	//change
	memcpy(g_installArgs.tmType, p_body, 20);
	p_body += 20;
	memcpy(g_installArgs.tmlId, p_body, 7);
	p_body += 7;
	g_vehicleArgs.PlateColor = *(UINT8 *)p_body;
	p_body++;

	int idx = 0;
	while (*p_body != 0x00 && idx < MAX_PLATE_NUM_LEN)
	{
		g_vehicleArgs.PlateNum[idx++] = *p_body;
		++p_body;
	}
	g_vehicleArgs.PlateNum[idx] = 0x00;
	//memcpy(g_vehicleArgs.PlateNum, p_body, MAX_PLATE_NUM_LEN);

/*	E(" provId: %x. city: %x. mfrId: %s. tmType: %s. tmlId: %s. color: %x. num: %s\n",	\
		g_vehicleArgs.ProvinceId, g_vehicleArgs.CityId, g_installArgs.mfrId, g_installArgs.tmType,	\
		g_installArgs.tmlId, g_vehicleArgs.PlateColor, g_vehicleArgs.PlateNum);
*/

	E(" mfrId: %s. tmType: %s.\n", g_installArgs.mfrId, g_installArgs.tmType);
	E(" tmlId: %s. color: %x. num: %s", g_installArgs.tmlId, g_vehicleArgs.PlateColor, g_vehicleArgs.PlateNum);
	//E(" tmlId: %s\n", g_installArgs.tmlId);
	return 0;
}

//0x0700
INT32 traver_record_up(UINT8 *pbMsg, TRAV_RECD_UP *trvRecd, INT32 clr, INT8 *lice)
{
	UINT8 *p_body = NULL;
	INT32 r, spd[10] = {0}, ix = 0;
	INT8 sql[BUF_SIZE] = {0}, *tm[10] = {NULL};
	UINT32 midVal = 0, msgLen = 0;
	INT32 drvNum, mile, speed, color, coefficient;
	INT8 carNum[18] = {0}, time[20] = {0}, end[20] = {0}, start[20] = {0};
	INT8 license[19] = {0}, number[255] = {0}, vin[255] = {0};

	if(pbMsg == NULL || trvRecd)
		return -1;
	p_body = pbMsg;

	trvRecd->seqNum = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	trvRecd->cmdWord = (*(UINT8 *)p_body++);
	msgLen = strlen((const char *)p_body);

	switch(trvRecd->cmdWord)
	{
		case 0x01:
			memcpy(&midVal, p_body, 3);
			p_body += 3;
			trvRecd->rd01.drvNum = ntohl(*(UINT32 *)&midVal);
			trvRecd->rd01.drvNum >>= 8;			
			memcpy(trvRecd->rd01.carNum, p_body, 18);
			p_body += 18;

			drvNum = trvRecd->rd01.drvNum;
			sprintf(carNum, "%s", trvRecd->rd01.carNum);
			sprintf(sql, "insert driverget values(null, '%d', '%s', '%s', now(), '%d');",	\
					clr, lice, carNum, drvNum);
			r = mysql_fun(sql);
			break;
		case 0x02:
			memcpy(trvRecd->rd02.clock, p_body, 6);
			p_body += 6;

			r = time_convert(trvRecd->rd02.clock, time);
			sprintf(sql, "insert clockget values(null, '%d', '%s', now(), '%s');", clr, lice, time);
			r = mysql_fun(sql);
			break;
		case 0x03:
			memcpy(&midVal, p_body, 3);
			p_body += 3;
			trvRecd->rd03.mile1 = ntohl(*(UINT32 *)&midVal);
			trvRecd->rd03.mile1 >>= 8; 
			memcpy(trvRecd->rd03.time1, p_body, 5);
			p_body += 5;

			mile = trvRecd->rd03.mile1;
			r = time_convert(trvRecd->rd03.time1, time);
			sprintf(sql, "insert mileget1 values(null, '%d', '%s', now(), '%s', '%d');",	\
					clr, lice, time, mile);
			r = mysql_fun(sql);
			break;
		case 0x04:
			memcpy(&midVal, p_body, 3);
			p_body += 3;
			g_installArgs.vehicleChrtCft = ntohl(*(UINT32 *)&midVal);
			g_installArgs.vehicleChrtCft >>= 8;

			coefficient = g_installArgs.vehicleChrtCft;
			sprintf(sql, "insert coefficientget values(null, '%d', '%s', '%d', now());", clr, lice, coefficient);
			r = mysql_fun(sql);
			break;
		case 0x05:
			trvRecd->rd05 = (RECORD_05H *)malloc(sizeof(RECORD_05H)*msgLen/6);
			if(trvRecd->rd05 == NULL){
				perror("malloc().");
				return -1;
			}
			for(ix = 0; ix < msgLen/6; ix++){
				memcpy(trvRecd->rd05[ix].time1, p_body, 5);
				p_body += 5;
				trvRecd->rd05[ix].speed1 = *(UINT8 *)p_body++;

				r = time_convert(trvRecd->rd05[ix].time1, time);
				speed = trvRecd->rd05[ix].speed1;
				sprintf(sql, "insert speedget1 values(null, '%d', '%s', now(), '%d', '%s');",	\
						clr, lice, speed, time);
				r = mysql_fun(sql);
				memset(sql, 0, sizeof(sql));
				memset(time, 0, sizeof(time));
			}
			free(trvRecd->rd05);
			break;
		case 0x06:
			memcpy(g_installArgs.vin, p_body, 17);
			p_body += 17;
			memcpy(g_installArgs.set.vehicle.PlateNum, p_body, 12);
			p_body += 12;
			memcpy(g_installArgs.plateType, p_body, 12);
			p_body += 12;

			sprintf(vin, "%s", g_installArgs.vin);
			sprintf(number, "%s", g_installArgs.set.vehicle.PlateNum);
			color = clr; //???
			sprintf(sql, "insert carget values(null, '%d', '%s', '%d', now(), '%s', '%s');",	\
				clr, lice, color, number, vin);
			r = mysql_fun(sql);
			break;
		case 0x07:
			for(ix = 0; ix < 10; ix++)
			{
				memcpy(trvRecd->rd07[ix].time, p_body, 6);
				p_body += 6;
				trvRecd->rd07[ix].speed = *(UINT8 *)p_body;
				p_body += 2;

				tm[ix] = (INT8 *)malloc(sizeof(INT8)*20);
				r = time_convert(trvRecd->rd07[ix].time, tm[ix]);
				spd[ix] = trvRecd->rd07[ix].speed;
			}
			sprintf(sql, "insert accidentget values(null, '%d', '%s', now(), '%d', '%d', '%d', '%d',	\
				'%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",	\
					clr, lice, spd[0], spd[1], spd[2], spd[3], spd[4], spd[5], spd[6], spd[7], spd[8], spd[9],	\
					tm[0], tm[1], tm[2], tm[3], tm[4], tm[5], tm[6], tm[7], tm[8], tm[9]);
			r = mysql_fun(sql);
			for(ix = 0; ix < 10; ix++)
			{
				free(tm[ix]);
			}
			break;
		case 0x08:
			memcpy(&midVal, p_body, 3);
			p_body += 3;
			trvRecd->rd08.mile2 = ntohl(*(UINT32 *)&midVal);
			trvRecd->rd08.mile2 >>= 8; 
			memcpy(trvRecd->rd08.time2, p_body, 5);
			p_body += 5;

			mile = trvRecd->rd08.mile2;
			r = time_convert(trvRecd->rd08.time2, time);
			sprintf(sql, "insert mileget2 values(null, '%d', '%s', now(), '%s', '%d');", clr, lice, time, mile);
			r = mysql_fun(sql);
			break;
		case 0x09:
			trvRecd->rd09 = (RECORD_09H *)malloc(sizeof(RECORD_09H)*msgLen/6);
			if(trvRecd->rd09 == NULL){
				perror("malloc().");
				return -1;
			}
			for(ix = 0; ix < msgLen/6; ix++){
				memcpy(trvRecd->rd09[ix].time2, p_body, 5);
				p_body += 5;
				trvRecd->rd09[ix].speed2 = *(UINT8 *)p_body++;

				r = time_convert(trvRecd->rd09[ix].time2, time);
				speed = trvRecd->rd09[ix].speed2;
				sprintf(sql, "insert speedget2(id, car_color, car_no, time, crdate, speed) values(null, %d',	\
					'%s', '%s', now(), '%d')", clr, lice, time, speed);
				r = mysql_fun(sql);
				memset(time, 0, sizeof(time));
			}
			free(trvRecd->rd09);
			break;
		case 0x11:
			trvRecd->rd11 = (RECORD_11H *)malloc(sizeof(RECORD_11H)*msgLen/28);
			if(trvRecd->rd11 == NULL){
				perror("malloc().");
				return -1;
			}
			for(ix = 0; ix < msgLen/28; ix++){
				memcpy(trvRecd->rd11[ix].carNum, p_body, 18);
				p_body += 18;
				memcpy(trvRecd->rd11[ix].start, p_body, 5);
				p_body += 5;
				memcpy(trvRecd->rd11[ix].end, p_body, 5);
				p_body += 5;

				sprintf(license, "%s", trvRecd->rd11[ix].carNum);
				r = time_convert(trvRecd->rd11[ix].start, start);
				r = time_convert(trvRecd->rd11[ix].end, end);
				sprintf(sql, "insert timeoutget values(null, '%d', '%s', now(), '%s', '%s', '%s');",	\
					clr, lice, end, license, start);
				r = mysql_fun(sql);

				memset(license, 0, sizeof(license));
				memset(start, 0, sizeof(start));
				memset(end, 0, sizeof(end));
			}
			free(trvRecd->rd11);
			break;
		default:
			break;
	}

	return 0;
}
//0x0003

//0x0102 termianl auth
INT32 term_authen(UINT8 *pbMsg)
{
	UINT8 *p_body = NULL;

	if(pbMsg == NULL)
		return -1;
	p_body = pbMsg;

	memcpy(g_installArgs.Akey, p_body, strlen((const char *)p_body));
	return 0;
}

//0x0301 ok!


INT32 store_data(TERM_ARGST *tmArgs, INT8 *sql)
{
	sprintf(sql, "insert searchterminalpar values(null, '%s', '%d', '%d');", tmArgs->cent, tmArgs->vid, tmArgs->tpid);
	if(mysql_fun(sql) == 1)
	{
		memset(sql, 0, strlen(sql) + 1);
		sprintf(sql, "update searchterminalpar set content = '%s' where vid = '%d' && tpid = '%d';", tmArgs->cent, tmArgs->vid, tmArgs->tpid);
		if(mysql_fun(sql) == 1)
			return -1;
	}
	return 0;
}	

INT32 encPkg_handle(JT808_MSG_PKG *pMsg, UINT8 *pb, INT32 sfd)
{
	static UINT16 seqNum = 0;
	static UINT16 pkgNum = 0;

	if(pMsg == NULL)
		return -1;

//	if(pMsg->head.attr & MSG_ENC_FLAG)
//	{
	if(pMsg->head.enc.num == 1)
	{
		if(p_SetArgsHead != NULL && p_SetArgsTail != NULL)
		{
			free(p_SetArgsHead);
			free(p_SetArgsTail);
		}
		p_SetArgsTail = (UINT8 *)malloc(1024*pMsg->head.enc.total);
		p_SetArgsHead = p_SetArgsTail;
		if(p_SetArgsHead == NULL)
		{
			perror("malloc().");
			free(p_SetArgsHead);

			seqNum = 0;
			pkgNum = 0;

			return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
		}
		memcpy(p_SetArgsTail, pMsg->pbody, (pMsg->head.attr & 0x3ff));
		p_SetArgsTail += (pMsg->head.attr & 0x3ff);
		pkgNum =  pMsg->head.enc.num;
		seqNum = pMsg->head.seqNum;

		return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_SUCCESS, sfd);
	}
	else if(pMsg->head.enc.num < pMsg->head.enc.total)
	{
		if(pMsg->head.enc.num == pkgNum + 1 )
		{
			memcpy(p_SetArgsTail, pMsg->pbody, ((pMsg->head.attr & 0x3ff)));
			p_SetArgsTail += (pMsg->head.attr & 0x3ff);
			pkgNum = pMsg->head.enc.num;
			seqNum = pMsg->head.seqNum;

			return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_SUCCESS, sfd);
		}
		else
		{
			pkgNum = 0;
			seqNum = 0;

			return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
		}
	}
	else if(pMsg->head.enc.num == pMsg->head.enc.total)
	{
		if((pMsg->head.enc.num == pkgNum + 1) )
		{
			memcpy(p_SetArgsTail, pMsg->pbody,(pMsg->head.attr & 0x3ff));
			p_SetArgsTail += (pMsg->head.attr & 0x3ff);

			pkgNum = 0;
			seqNum = 0;                                 
			setArgsFinishFlag = TRUE;
		}
		else
		{
			pkgNum = 0;
			seqNum = 0;

			return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
		}

		if(setArgsFinishFlag == TRUE && p_SetArgsHead != NULL){
			pb = p_SetArgsHead;
		}
		else{
			pb = pMsg->pbody;
		}

	}
//	}//end if pMsg->head.att

	return 0;
}

//0x0104
INT32 search_term_argRpy(JT808_MSG_PKG *pMsg, SET_ARGS_STR *pArgs, INT32 vid, INT32 sfd)
{
	UINT8 *p_body = NULL;
	UINT8 argTotal, argLen, ix;
	INT8 cmd_sql[256] = {0}, hanZi[256] = {0};
	UINT32 argId, r = 0;
	TERM_ARGST termArg;
	

	if(pMsg == NULL || pArgs == NULL)
		return -1;

	if(setArgsFinishFlag == TRUE && p_SetArgsHead != NULL){
		//seqNum = (UINT16)ntohs(*(UINT16 *)p_SetArgsHead);
		argTotal = p_SetArgsHead[2];
		p_body = p_SetArgsHead + 3;
	}
	else{
		p_body = pMsg->pbody;
		p_body += 2;
		argTotal = *p_body++;
	}
	for(ix = 0; ix < argTotal; ix++){
		argId = ntohl(*(UINT32 *)p_body);
		p_body += 4;
		argLen = *(UINT8 *)p_body;
		p_body++;

		memset(cmd_sql, 0, sizeof(cmd_sql));
		memset(termArg.cent, 0, sizeof(sizeof(termArg.cent)));
		memset(hanZi, 0, sizeof(hanZi));

		termArg.vid = vid;
		termArg.tpid = argId;

		switch(argId){
			case 0x0001:
				pArgs->ack.beatInterval = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.beatInterval);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0002:
				pArgs->ack.tcpAckTimeout = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.tcpAckTimeout);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0003:
				pArgs->ack.tcpRetransTimes = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.tcpRetransTimes);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0004:
				pArgs->ack.udpAckTimeout = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.udpAckTimeout);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0005:
				pArgs->ack.udpRetransTimes = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.udpRetransTimes);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0006:
				pArgs->ack.smsAckTimeout = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.smsAckTimeout);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0007:
				pArgs->ack.smsRetransTimes = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->ack.smsRetransTimes);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0010:
				memcpy(pArgs->server.mainApn, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.mainApn);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0011:
				memcpy(pArgs->server.mainUsr, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.mainUsr);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0012:
				memcpy(pArgs->server.mainCode, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.mainCode);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0013:
				memcpy(pArgs->server.mainIp, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.mainIp);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0014:
				memcpy(pArgs->server.backupApn, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.backupApn);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0015:
				memcpy(pArgs->server.backupUsr, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.backupUsr);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0016:
				memcpy(pArgs->server.backupCode, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.backupCode);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0017:
				memcpy(pArgs->server.backupIp, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->server.backupIp);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0018:
				pArgs->server.tcpPort = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->server.tcpPort);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0019:
				pArgs->server.udpPort = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->server.udpPort);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0020:
				pArgs->report.rptWay = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.rptWay);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0021:
				pArgs->report.rptStatus = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.rptStatus);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0022:
				pArgs->report.NoLogTmRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.NoLogTmRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0027:
				pArgs->report.sleepTmRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.sleepTmRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0028:
				pArgs->report.alarmTmRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.alarmTmRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0029:
				pArgs->report.dftTmRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.dftTmRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x002C:
				pArgs->report.dftDisRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.dftTmRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x002D:
				pArgs->report.NoLogDisRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.NoLogDisRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x002E:
				pArgs->report.sleepDisRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.sleepDisRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x002F:
				pArgs->report.alarmDisRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.alarmDisRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0030:
				pArgs->report.angleRpt = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->report.angleRpt);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0040:
				memcpy(pArgs->phone.centreTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.centreTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0041:
				memcpy(pArgs->phone.resetTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.resetTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0042:
				memcpy(pArgs->phone.facSetTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.facSetTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0043:
				memcpy(pArgs->phone.centreSmsTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.centreSmsTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0044:
				memcpy(pArgs->phone.centreSmsAlarmTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.centreSmsAlarmTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0045:
				pArgs->phone.answerWay = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->phone.answerWay);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0046:
				pArgs->phone.MaxcallTmEach = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->phone.MaxcallTmEach);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0047:
				pArgs->phone.MaxcallTmMon = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->phone.MaxcallTmMon);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0048:
				memcpy(pArgs->phone.monitorTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.monitorTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0049:
				memcpy(pArgs->phone.privilegeSmsTel, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->phone.privilegeSmsTel);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;

			case 0x0050:
				pArgs->alarm.alarmMask = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.alarmMask);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0051:
				pArgs->alarm.alarmSmsFlag = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.alarmSmsFlag);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0052:
				pArgs->alarm.alarmCmrFlag = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.alarmCmrFlag);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0053:
				pArgs->alarm.alarmCmrSaveFlag = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.alarmCmrSaveFlag);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0054:
				pArgs->alarm.alarmKeyFlag = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.alarmKeyFlag);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0055:
				pArgs->alarm.maxSpeed = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.maxSpeed);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0056:
				pArgs->alarm.overSpeedTm = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.overSpeedTm);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0057:
				pArgs->alarm.conDrvTm = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.conDrvTm);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0058:
				pArgs->alarm.totalDrvTm = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.totalDrvTm);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0059:
				pArgs->alarm.minRestTm = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.minRestTm);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x005A:
				pArgs->alarm.MaxStopTm = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->alarm.MaxStopTm);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0070:
				pArgs->pic.picQuality = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->pic.picQuality);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0071:
				pArgs->pic.light = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->pic.light);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0072:
				pArgs->pic.contrast = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->pic.contrast);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0073:
				pArgs->pic.saturation = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->pic.saturation);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0074:
				pArgs->pic.chroma = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->pic.chroma);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;

			case 0x0080:
				pArgs->vehicle.mileage = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->vehicle.mileage);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0081:
				pArgs->vehicle.ProvinceId = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->vehicle.ProvinceId);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0082:
				pArgs->vehicle.CityId = ntohl(*(UINT32 *)(p_body));
				sprintf(termArg.cent, "%u", pArgs->vehicle.CityId);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body += 4;	
				break;
			case 0x0083:
				memcpy(pArgs->vehicle.PlateNum, p_body, argLen);
				p_body += argLen;
				sprintf(hanZi, "%s", pArgs->vehicle.PlateNum);
				memcpy(termArg.cent, hanZi, argLen);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				break;
			case 0x0084:
				pArgs->vehicle.PlateColor = *(UINT8 *)p_body;
				sprintf(termArg.cent, "%u", pArgs->phone.MaxcallTmMon);

				r = store_data(&termArg, cmd_sql);	
				if(r == -1)
					return platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_FAIL, sfd);
				p_body++;	
				break;
			default:
				r = platform_common_ack(pMsg->head.seqNum, pMsg->head.id, COM_ACK_UNSUPPORT, sfd);
		}//end switch
	}//end for
	if(setArgsFinishFlag == TRUE && p_SetArgsHead != NULL)
	{
		setArgsFinishFlag = FALSE;
		p_SetArgsTail = NULL;
		free(p_SetArgsHead);
		p_SetArgsHead = NULL;
	}

	return platform_common_ack(pMsg->head.seqNum, pMsg->head.id,COM_ACK_SUCCESS, sfd);
}


//0x0200
INT32 posInfo_report(JT808_MSG_PKG *pMsg, POS_INFO *pPos)
{
	UINT8 *p_body = NULL;
	UINT8 id, len = 0;
	INT32 pkgLen = 0;

	if(pMsg == NULL || pPos == NULL)
		return -1;
	p_body = pMsg->pbody;

	pPos->base.alarmFlag = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	pPos->base.status = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	pPos->base.gps.latitude = ntohl(*(UINT32 *)p_body);//float
	p_body += 4;
	pPos->base.gps.longitude = ntohl(*(UINT32 *)p_body);//float
	p_body += 4;
	pPos->base.gps.height = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	pPos->base.gps.speed = ntohs(*(UINT16 *)p_body);//float
	p_body += 2;
	pPos->base.gps.course = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	memcpy(pPos->base.gps.tm, p_body, sizeof(pPos->base.gps.tm));
	p_body += 6;

	pkgLen = pMsg->head.attr & 0x03ff;

	if(pkgLen > sizeof(POS_BASIC))
	{
		while(*p_body){

			id = *(UINT8 *)p_body;
			p_body++;
			len = *(UINT8 *)p_body;
			p_body++;

			switch(id){
				case 0x01:
					pPos->add.mileage = ntohl(*(UINT32 *)p_body);
					p_body += 4;
					break;
				case 0x02:
					pPos->add.fuel = ntohs(*(UINT16 *)p_body);
					p_body += 2;
					break;
				case 0x03:
					pPos->add.speed = ntohl(*(UINT16 *)p_body);
					p_body += 2;
					break;
				case 0x11:
					if(len == 5){
						pPos->add.overSpdAdd.type = *(UINT8 *)p_body;
						p_body++;
						pPos->add.overSpdAdd.id = ntohl(*(UINT32 *)p_body);
						p_body += 4;
					}
					else if(len == 1){
						pPos->add.overSpdAdd.type = *(UINT8 *)p_body;
						p_body++;
					}
					break;
				case 0x12:
					pPos->add.areaAdd.type = *(UINT8 *)p_body;
					p_body++;
					pPos->add.areaAdd.id = ntohl(*(UINT32 *)p_body);
					p_body += 4;
					pPos->add.areaAdd.drc = *(UINT8 *)p_body;
					p_body++;
					break;
				case 0x13:
					pPos->add.drvTm.id = ntohl(*(UINT32 *)p_body);
					p_body += 4;
					pPos->add.drvTm.tm = ntohs(*(UINT16 *)p_body);
					p_body += 2;
					pPos->add.drvTm.result = *(UINT8 *)p_body;
					p_body++;
					break;
				default:
					break;
			}//end switch ...
		}//end while ...
	}//end if ...
	return 0;
}

//0x0201
INT32 posInfo_search_rpy(JT808_MSG_PKG *pMsg, POSINFO_SERCHRPY *pPos_sech)
{
	UINT8 *p_body = NULL;
	UINT16 seqNum;

	if(pMsg == NULL || pPos_sech == NULL)
		return -1;
	p_body = pMsg->pbody;

	seqNum = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	pPos_sech->posInfo.base.alarmFlag = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	pPos_sech->posInfo.base.status = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	pPos_sech->posInfo.base.gps.latitude = ntohl(*(UINT32 *)p_body);//float
	p_body += 4;
	pPos_sech->posInfo.base.gps.longitude = ntohl(*(UINT32 *)p_body);//float
	p_body += 4;
	pPos_sech->posInfo.base.gps.height = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	pPos_sech->posInfo.base.gps.speed = ntohs(*(UINT16 *)p_body);//float
	p_body += 2;
	pPos_sech->posInfo.base.gps.course = ntohs(*(UINT16 *)p_body);
	p_body += 2;
	memcpy(pPos_sech->posInfo.base.gps.tm, p_body, sizeof(pPos_sech->posInfo.base.gps.tm));
	p_body += 6;

	return 0;
}



//0x0701
INT32 elec_bill_up(UINT8 *pbMsg, ELEC_BILL_UP *elecBill)
{
	UINT8 *p_body = NULL;

	if(pbMsg == NULL || elecBill == NULL)
		return -1;
	p_body = pbMsg;

	elecBill->elcBillLen = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	memcpy(elecBill->elcBillCtent, p_body, (size_t)elecBill->elcBillLen);

	return 0;
}

//0x0702
INT32 drvInfo_upload(UINT8 *pbMsg, DRIVER_INFO *drvInfo)
{
	UINT8 *p_body = NULL;
	UINT8 datLen;

	if(pbMsg == NULL || drvInfo == NULL)
		return -1;
	p_body = pbMsg;

	datLen = *(UINT8 *)p_body++;
	memcpy(drvInfo->name, p_body, datLen);
	p_body += datLen;
	memcpy(drvInfo->IDcard, p_body, 20);
	p_body += 20;
	memcpy(drvInfo->drvNum, p_body, 40);
	p_body += 40;
	datLen = *(UINT8 *)p_body++;
	memcpy(drvInfo->orgName, p_body, datLen);

	return 0;	
}

//0x0800
INT32 media_event_upload(UINT8 *pbMsg, MEDIA_EVENT_UP *medEvent)
{
	UINT8 *p_body = NULL;

	if(pbMsg == NULL || medEvent == NULL)
		return -1;
	p_body = pbMsg;

	medEvent->mediaDatID = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	medEvent->mediaType = *(UINT8 *)p_body++;
	medEvent->mediaCode = *(UINT8 *)p_body++;	
	medEvent->eventCode = *(UINT8 *)p_body++;	
	medEvent->channelID = *(UINT8 *)p_body++;

	return 0;	
}

//0x0801
INT32 media_data_upload(UINT8 *pbMsg, MEDIA_DAT_UP *medDat)
{
	UINT8 *p_body = NULL;

	if(pbMsg == NULL || medDat == NULL)
		return -1;
	p_body = pbMsg;

	medDat->mediaDatID = ntohl(*(UINT32 *)p_body);
	p_body += 4;
	medDat->mediaType = *(UINT8 *)p_body++;
	medDat->mediaCode = *(UINT8 *)p_body++;	
	medDat->eventCode = *(UINT8 *)p_body++;	
	medDat->channelID = *(UINT8 *)p_body++;
	memcpy(medDat->mediaPkg, p_body, sizeof(medDat->mediaPkg));	

	return 0;
}
